﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class DrawingBoardViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public UndoRedoController UndoRedoController { get { return currentLayer == null ? null : currentLayer.UndoRedoController; } }
        public double Width { get; set; }
        public double Height { get; set; }

        private DrawingBoardXnaComponent xnaComponent;
        private TextureData currentLayer;
        private Point previousPosition;
        private Microsoft.Xna.Framework.Graphics.Texture2D previousTexture;

        public DrawingBoardViewModel(DrawingBoardXnaComponent xnaComponent)
        {
            this.xnaComponent = xnaComponent;
        }

        public void LoadDrawTool(IDrawTool drawTool)
        {
            xnaComponent.DrawTool = drawTool;
        }

        public void RenderPoint(Point position, float pressurePercentage)
        {
            if (currentLayer == null)
                return;
            
            previousTexture = currentLayer.Texture.TextureItem;
            Point convertedPosition = AdjustPoint(position);

            xnaComponent.RenderPoint(convertedPosition, currentLayer.Texture, pressurePercentage);
            previousPosition = convertedPosition;
            OnPropertyChanged("UndoRedoController");
        }

        public void RenderLine(Point position, float pressurePercentage)
        {
            if (currentLayer == null)
                return;

            Point convertedPosition = AdjustPoint(position);

            xnaComponent.RenderLine(convertedPosition, previousPosition, currentLayer.Texture, pressurePercentage);
            previousPosition = convertedPosition;
            OnPropertyChanged("UndoRedoController");
        }

        public void CreateUndoPoint()
        {
            if (currentLayer == null || xnaComponent.DrawTool == null)
                return;

            currentLayer.UndoRedoController.AddItem(new UndoRedoTextureItem(currentLayer.Texture, previousTexture));
            OnPropertyChanged("UndoRedoController");
        }

        private Point AdjustPoint(Point point)
        {
            //return new System.Windows.Point((point.X - xnaComponent.CenterPosition.X + xnaComponent.CurrentFrame.OriginX * xnaComponent.Scale) / xnaComponent.Scale, (point.Y - xnaComponent.CenterPosition.Y + xnaComponent.CurrentFrame.OriginY * xnaComponent.Scale) / xnaComponent.Scale);
            return new System.Windows.Point(((point.X - xnaComponent.CenterPosition.X) / xnaComponent.Scale) + xnaComponent.CurrentFrameData.Frame.OriginX,
                ((point.Y - xnaComponent.CenterPosition.Y) / xnaComponent.Scale) + xnaComponent.CurrentFrameData.Frame.OriginY);
        }

        public void SetScale(int scalePercentage)
        {
            xnaComponent.Scale = (float)scalePercentage / 100;
        }

        public void SetAnimationSelectionInfo(AnimationSelectionInfo animationSelectionInfo)
        {
            xnaComponent.AnimationSelectionInfo = animationSelectionInfo;
        }

        public void SetCurrentLayer(TextureData layer)
        {
            currentLayer = layer;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
